//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.ComponentModel;
using System.Workflow.ComponentModel;
using Microsoft.Research.ScientificWorkflow;

namespace Microsoft.Research.ScientificWorkflow.Activities
{
    [Name("Activity for generating a random integrer not greater than a preset MAX value", "Integer Random Generator")]
    [Description("Generates a random integer between 0 and the value set as argument to MAX value.")]
    [WorkflowCategory("Utilities")]
    public class RandomInt : System.Workflow.ComponentModel.Activity
    {
        public RandomInt()
        {
        }

        public static readonly System.Workflow.ComponentModel.DependencyProperty MaxValueProperty =
            System.Workflow.ComponentModel.DependencyProperty.Register("MaxValue", typeof(int), 
            typeof(RandomInt));

        [RequiredInputParam]
        [Name("Max Value")]
        [Description("The maximum value till which the generated random number can reach.")]
        public int MaxValue
        {
            get { return (int)GetValue(RandomInt.MaxValueProperty); }
            set { SetValue(RandomInt.MaxValueProperty, value); }
        }


        public static System.Workflow.ComponentModel.DependencyProperty OutputProperty =
            System.Workflow.ComponentModel.DependencyProperty.Register("Output", typeof(int), 
            typeof(RandomInt));

        [OutputParam]
        [Name("Output")]
        [Description("The generated random number.")]
        public int Output
        {
            get { return (int)GetValue(RandomInt.OutputProperty); }
            set { SetValue(RandomInt.OutputProperty, value); }
        }


        private int result = -1;


        protected override ActivityExecutionStatus  Execute(ActivityExecutionContext executionContext)
        {
            if (result == -1)
            {
                Random r = new Random();
                result = r.Next(MaxValue);
            }
            this.Output = result;
            Console.Out.WriteLine("Random '{0}' is '{1}'", this.Name, result);

            return ActivityExecutionStatus.Closed;
        }
    }
}
